package notes.java.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import com.alibaba.fastjson.JSONObject;

// 我们在统计计算时，常会用到，
//            1、调用用户接口获取获取用户信息，
//            2、调用余额接口获取用户余额信息，
//            3、调用花呗接口获取花呗信息
// 如果上面三个接口串行调用，将非常耗时，利用率很低，这时我们可以用FutureTask来解决
public class T2_FutureTask {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// 要调用的接口
		UserService userService = new UserServiceImpl();
		MoneyService moneyService = new MoneyServiceImpl();
		HuabeiService huabeiService = new HuabeiServiceImpl();
		
		// 调用用户信息的线程
		Callable<JSONObject> userThread = new Callable<JSONObject>() {
			@Override
			public JSONObject call() throws Exception {
				return userService.getUserInfo();
			}
		};
		
		// 调用用户余额的线程
		Callable<JSONObject> moneyThread = new Callable<JSONObject>() {
			@Override
			public JSONObject call() throws Exception {
				return moneyService.getMoneyInfo();
			}
		};
		
		// 调用用户花呗的线程
		Callable<JSONObject> huabeiThread = new Callable<JSONObject>() {
			@Override
			public JSONObject call() throws Exception {
				return huabeiService.getHuabeiInfo();
			}
		};
		
		// 用户信息任务
		FutureTask<JSONObject> userFutureTask = new FutureTask<JSONObject>(userThread);
		// 余额任务
		FutureTask<JSONObject> moneyFutureTask = new FutureTask<JSONObject>(moneyThread);
		// 花呗任务
		FutureTask<JSONObject> huabeiFutureTask = new FutureTask<JSONObject>(huabeiThread);
		
		// 创建一个线程池，把任务放进线程池
		ExecutorService executorService = Executors.newCachedThreadPool();
		executorService.submit(userFutureTask);
		executorService.submit(moneyFutureTask);
		executorService.submit(huabeiFutureTask);
		
		// 获取结果
		JSONObject userInfo = userFutureTask.get();
		JSONObject moneyInfo = moneyFutureTask.get();
		JSONObject huabeiInfo = huabeiFutureTask.get();
		
		System.out.println(userInfo.toJSONString());
		System.out.println(moneyInfo.toJSONString());
		System.out.println(huabeiInfo.toJSONString());
	}
	
}

// 模拟用户接口获取用户信息
interface UserService {
	public JSONObject getUserInfo();
}
class UserServiceImpl implements UserService {
	@Override
	public JSONObject getUserInfo() {
		JSONObject obj = new JSONObject();
		obj.put("name", "张三");
		obj.put("age", 23);
		return obj;
	}
}
// 模拟余额接口获取余额信息
interface MoneyService {
	public JSONObject getMoneyInfo();
}
class MoneyServiceImpl implements MoneyService {
	@Override
	public JSONObject getMoneyInfo() {
		JSONObject obj = new JSONObject();
		obj.put("money", 9999.99);
		return obj;
	}
}
// 模拟花呗接口获取花呗信息
interface HuabeiService {
	public JSONObject getHuabeiInfo();
}
class HuabeiServiceImpl implements HuabeiService {
	@Override
	public JSONObject getHuabeiInfo() {
		JSONObject obj = new JSONObject();
		obj.put("huankuan", 1000.12);
		return obj;
	}
	
}